package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 订单明细控制层
 * @author Xiao
 * @date 2021/9/28
 */
@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {
    @Autowired
    private OrderService orderService;
    /**
     * 分页查询订单
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/page")
    public R<Page<Orders>> getOrderDetailPage(
            @RequestParam Integer page, Integer pageSize, String number,
            String beginTime, String endTime) {

        log.info("page:{}, pageSize:{}, number:{}, beginTime:{}, endTime:{}",
                page, pageSize, number, beginTime, endTime);

        Page<Orders> ordersPage = orderService.getOrderDetailPage(
                page, pageSize, number, beginTime, endTime);

        return R.success(ordersPage);
    }
    /**
     * 修改订单状态
     * @param orders
     * @return
     */
    @PutMapping
    public R<String> editOrderDetail(@RequestBody Orders orders) {
        log.info("orders:{}", orders);
        orderService.editOrderDetail(orders);
        return R.success("修改成功！");
    }
    /**
     * 提交订单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> addOrderApi(@RequestBody Orders orders) {
        log.info("orders:{}", orders);
        orderService.addOrderApi(orders);
        return R.success("提交成功！");
    }
    /**
     * 分页查询订单
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    public R<Page<OrdersDto>> orderPagingApi(@RequestParam Integer page, Integer pageSize) {
        Page<OrdersDto> ordersDtoPage = orderService.orderPagingApi(page, pageSize);
        return R.success(ordersDtoPage);
    }
    /**
     * 再来一单
     * @param orders
     * @return
     */
    @PostMapping("/again")
    public R<String> orderAgainApi(@RequestBody Orders orders) {
        orderService.orderAgainApi(orders);
        return R.success("再来一单！");
    }
}
